<?php

namespace app\admin\controller;

use wechat\src\WeChat;

/**
 * 微信用户设置，
 */
class Wechatgroupfa extends base
{

    protected $wechat;

    public function _initialize()
    {
        $this->wechat = (new WeChat);
        $this->wechat->valid();

    }

    /**
        群
    */
    public function index(){

        $data= db('wgroups')->select();
        if(empty($data)){
            $group = $this->wechat->instance('group')->getAllGroups();
        }

        if(!empty($group)){
            foreach($group['groups'] as $key =>$value){
                $list[$key]['id'] = $value['id'];
                $list[$key]['name'] = $value['name'];
            }
            db('wgroups')->insertAll($list);
        }

        $this->assign('data', $data);
        return $this->fetch();
    }

    /**
    用户列表
    */
    public function userlist(){
		
		/** 写入数据库*
            $data = $this->wechat->instance('user')->getUserLists();
            $value['user_list'] = null;
            foreach ($data['data']['openid'] as $key) {
                $value['user_list'][] = [
                    "openid" => "$key",
                    "lang" => "zh_CN",
                ];
            }
            $user = $this->wechat->instance('user')->getUserInfoLists($value);

		foreach($user['user_info_list'] as $key =>$value){
			
			$list[$key]['openid'] = $value['openid'];
			$list[$key]['groupid'] = $value['groupid'];
			$list[$key]['subscribe'] = $value['subscribe'];
			$list[$key]['nickname'] = $value['nickname'];
			$list[$key]['sex'] = $value['sex'];
			$list[$key]['city'] = $value['city'];
			$list[$key]['country'] = $value['country'];
			$list[$key]['province'] = $value['province'];
			$list[$key]['headimgurl'] = $value['headimgurl'];
			$list[$key]['subscribe_time'] = $value['subscribe_time'];
		}
			$res= db('wuser')->insertAll($list);
			
            /**/

        $list = db('wuser')->where('groupid',$this->request->param('groupid'))->paginate();

        $wgroups= db('wgroups')->select();
        $this->assign('wgroups', $wgroups);
        $this->assign('list', $list);
        $this->assign('num', db('wuser')->where('groupid',$this->request->param('groupid'))->count());
        return $this->fetch();

    }


    /**
    添加群
    */
    public function add(){
        return $this->fetch('edit');
    }

    /**
    编辑群
    */
    public function edit($id){
        return $this->fetch();
    }


    /**
    删除群
    */
    public function del($id){
         $jsonstr = ["group"=>
                        ["id"=>$id]
                    ];
            $data = $this->wechat->instance('group')->delGroup($jsonstr);

            if($data =='1'){
                    db('wgroups')->where('id',$id)->delete();
                   $this->success('删除成功');
                } else {
                    $this->error($data['errmsg'].'删除失败');
                }
    }


    /**
    保存群
    */
    public function save(){
        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }
        if($this->request->post('id')){
                             $jsonstr = ["group"=>
                        ["id"=>$this->request->post('id'),"name"=>$this->request->post('name')]
                                ];
                        $data = $this->wechat->instance('group')->changeGroupName($jsonstr);
             
        }else{
                    $jsonstr = ["group"=>
                        ["name"=>$this->request->post('name')]
                    ];
                    $data = $this->wechat->instance('group')->create($jsonstr);
    
        }

           if(isset($data['group'])||$data =='1'){
                if($this->request->post('id')){
                   db('wgroups')->where(['id'=>$this->request->post('id')])->update(['name' => $this->request->post('name')]); 
                   }else{
                        db('wgroups')->insert(["id"=>$data['group']['id'],'name' =>$data['group']['name']]); 
                   }
                    

                    return ajax_info('保存成功','u');
                }else{
                    return ajax_info($data['errmsg'] . ',没有修改','u');
                }


    }



    /**
     * 移动用户
     */
    public function move_user()
    {

        $post = $this->request->post();
        if (empty($post['openid'])) {
            return ajax_info('请选择用户！', 'index');
        }

        $list = array();
        foreach ($post['openid'] as $key=>$value) {
            
                    $list["openid_list"][$key] = $value;
                }
                    
		$groupid = ["to_groupid"=>$post['groupsid']];
					
                  $jsonstr = array_merge($list,$groupid);


        $data = $this->wechat->instance('group')->moveUserToGroup($jsonstr);
        if($data =='1'){

                  foreach ($list['openid_list'] as $key => $value) {
                     db('wuser')->where(['openid'=>$value])->update(['groupid'=>$post['groupsid']]);
                      }
                       return ajax_info('用户移动成功','u');
        }else{
             return ajax_info($data['errmsg'] . ',没有移动','u');
        }
    }


    /**
     * 发送消息
     */
    public function save11()
    {
        //指定用户
//        $data = "bbbbbbfdfdsvkcxvxc大师傅大师傅";
//        $res  = $this->wechat->instance('Customservice')->sendImage("otuUG0ztt5Wl_KnUs0T_dK9_2tXg",$data);
//        dump($res);
//        exit();
//        //指定用户
//        $data = "bbbbbbfdfdsvkcxvxc大师傅大师傅";
//        $res  = $this->wechat->instance('Customservice')->sendTest("otuUG0ztt5Wl_KnUs0T_dK9_2tXg",$data);
//        dump($res);
//        exit();

        //可以群发信息了

        $data = [
            "filter"			=> [
                "is_to_all" 	=> true,
            ],

            "text"              => [
                "content" => "你觉得呢？！成功了！要怎么利用呢？!1"
            ],
            "msgtype"             => "text"
        ];
        $res  = $this->wechat->instance('message')->sendAll($data);
        dump($res);
        exit();
        $dd =$this->wechat->instance('material')->upload('image','../public/uploads/sm_avatar.png',1);
//        $dd =$this->wechat->instance('material')->total();
        dump($dd);
        exit();
        $data = [
            "touser"  => "otuUG0ztt5Wl_KnUs0T_dK9_2tXg",
            "mpnews"  => [
                "media_id" => "vy-Bt5ys21PaDtHwY8DXhW2Z7KDpIU9_S1k2lA-4FGfDSoxdZBUkmM-Q9JFk-nRq",
            ],
            "msgtype" => "mpnews",
        ];


        $aa = $this->wechat->instance('message')->preview($data);
        dump($aa);

    }
}
